var KOC = require("koc.common.utils");

var StateData = require("../statedata");
var DALFactory = require("../../lib/dal/dal-factory");
var CommonUtils = require("../../lib/common-utils");

module.exports = {
    User: {
        Avatar: function (req, res, callback) {
            res.render("member/user/avatar", callback);
        },
        Cover: function (req, res, callback) {
            res.render("member/user/cover", callback);
        },
        Change_Password: function (req, res, callback) {
            res.render("member/user/change-password", callback);
        },
        Bound_ThirdParty: function (req, res, callback) {
            res.render("member/user/user-bound-qqwei", callback);
        },
        Bound_Email: function (req, res, callback) {
            res.render("member/user/user-bound-email", callback);
        },
        Cash: function (req, res, callback) {
            var _UserState = StateData.Value(req, res);
            var _PageData = {
                flag: 0,
                message: ""
            };
            DALFactory.User.User_Info.Info(_UserState.User_ID, function (retValue) {
                if (retValue.hasError) {
                    _PageData.flag = retValue.errorCode;
                    _PageData.message = retValue.message;
                } else if (!retValue.returnObject) {
                    _PageData.flag = -1;
                    _PageData.message = '没有相应参数';
                } else {
                    _PageData.UserInfo = {};
                    _PageData.UserInfo.Beans = retValue.returnObject.Beans;
                    //判断时间
                    if (retValue.returnObject.ApplyMoney_LastDate) {
                        var last_date = retValue.returnObject.ApplyMoney_LastDate;
                        var last_day = KOC.Moment(last_date).date();
                        var now_day = KOC.Moment().date();
                        if (KOC.Moment(last_date).year() == KOC.Moment().year() &&
                            KOC.Moment(last_date).month() == KOC.Moment().month()) {
                            //同年同月
                            if (last_day <= 10 && now_day <= 10) {
                                _PageData.UserInfo.Hint = '上旬只能提现一次';
                            }
                            if (last_day > 10 && last_day <= 20 && now_day > 10 && now_day <= 20) {
                                _PageData.UserInfo.Hint = '中旬只能提现一次';
                            }
                            if (last_day > 20 && now_day > 20) {
                                _PageData.UserInfo.Hint = '下旬只能提现一次';
                            }
                        }
                    }
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("member/anchor/cash", callback);
            });

        },
        IncomeInfo: function (req, res, callback) {
            var _UserState = StateData.Value(req, res);
            var _PageData = {
                flag: 0,
                message: ""
            };
            if (!req.params.id) {
                _PageData.flag = -1;
                _PageData.message = '没有相应参数';
                res.render("member/family/anchor_live_detail", callback);
            }
            _PageData.Report_Day = req.params.id;
            DALFactory.User.Family_Info.Info_UserID(_UserState.User_ID, function (retValue) {
                if (retValue.hasError) {
                    _PageData.flag = retValue.errorCode;
                    _PageData.message = retValue.message;
                } else if (!retValue.returnObject) {
                    _PageData.flag = -1;
                    _PageData.message = '没有相应参数';
                } else {
                    _PageData.Family_ID = retValue.returnObject.Family_ID;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("member/family/anchor_income_detail", callback);
            });

        },
        LiveInfo: function (req, res, callback) {
            var _PageData = {
                flag: 0,
                message: ""
            };
            if (!req.params.id) {
                _PageData.flag = -1;
                _PageData.message = '没有相应参数';
                res.render("member/family/anchor_live_detail", callback);
            }
            _PageData.User_ID = req.params.id;
            if (!req.params.Report_Day) {
                _PageData.flag = -1;
                _PageData.message = '没有相应参数';
                res.render("member/family/anchor_live_detail", callback);
            }
            _PageData.Report_Day = req.params.Report_Day;
            KOC.Async.series([
                //艺人总时长
                function (callback) {
                    DALFactory.User.User_Anchor.Info(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        if (!retValue.returnObject) {
                            _PageData.flag = retValue.errorCode;
                            _PageData.message = retValue.message;
                            callback(retValue);
                            return;
                        }
                        _PageData.Live_Duration_Total = retValue.returnObject.Live_Duration_Total;
                        _PageData.Family_ID=retValue.returnObject.Family_ID;
                        callback();
                    });
                },
                //艺人直播时长和开播时间
                function (callback) {
                    DALFactory.User.User_Anchor.Info_Live_Day(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        if (!retValue.returnObject) {
                            _PageData.flag = retValue.errorCode;
                            _PageData.message = retValue.message;
                            callback(retValue);
                            return;
                        }
                        _PageData.Duration = retValue.returnObject.Duration;
                        _PageData.Beans_Anchor=retValue.returnObject.Beans_Anchor;
                        _PageData.Create_Date = retValue.returnObject.Create_Date;
                        callback();
                    });
                },
                //实时有效直播时长
                function(callback){
                    DALFactory.User.User_Anchor.Info_Duration_Efftive(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        if (!retValue.returnObject) {
                            _PageData.flag = retValue.errorCode;
                            _PageData.message = retValue.message;
                            callback(retValue);
                            return;
                        }
                        _PageData.Live_Duration_Efftive = retValue.returnObject.Live_Duration_Efftive;
                        _PageData.Live_Count=retValue.returnObject.Live_Count;
                        callback();
                    }); 
                }
            ], function (retValue) {
                if (retValue) {
                    _PageData.flag = retValue.errorCode;
                    _PageData.message = retValue.message;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("member/family/anchor_live_detail", callback);
            });
        },
        Report: function (req, res, callback) {
            ////////////////////////////////////////////
            //Report
            ////////////////////////////////////////////
            var _UserState = StateData.Value(req, res);
            var _PageData = {
                flag: 0,
                message: ""
            };
            DALFactory.User.User_Info.Info(req.params.id, function (retValue) {
                if (retValue.hasError) {
                    _PageData.flag = retValue.errorCode;
                    _PageData.message = retValue.message;
                } else if (!retValue.returnObject) {
                    _PageData.flag = -1;
                    _PageData.message = '没有相应参数';
                } else {
                    _PageData.UserInfo = {};
                    _PageData.UserInfo.NickName = retValue.returnObject.NickName;
                    _PageData.UserInfo.User_ID = retValue.returnObject.User_ID;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("room/report", callback);
            });
        },
        Exchange: function (req, res, callback) {
            var _UserState = StateData.Value(req, res);
            var _PageData = {
                flag: 0,
                message: ""
            };
            DALFactory.User.User_Info.Info(_UserState.User_ID, function (retValue) {
                if (retValue.hasError) {
                    _PageData.flag = retValue.errorCode;
                    _PageData.message = retValue.message;
                } else if (!retValue.returnObject) {
                    _PageData.flag = -1;
                    _PageData.message = '没有相应参数';
                } else {
                    _PageData.NickName = retValue.returnObject.NickName;
                    _PageData.User_ID = retValue.returnObject.User_ID;
                    _PageData.Beans=retValue.returnObject.Beans;
                    _PageData.Coin =retValue.returnObject.Coin;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("member/user/exchange", callback);
            });

        },
    }
};